<?php
namespace app\common\model;
use think\Model;
use think\Db;
/**
* 
*/
class Projects extends Model
{
	//自动写入创建和更新的时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'add_time';
    protected $updateTime = 'edit_time';


    protected function initialize()
    {
        parent::initialize();
         header("Content-type: text/html; charset=utf-8");
    }

    public function addData($param)
    {
    	if (empty($param)) {
            $this->error = '参数错误';
            return false;
        }
        if (empty($param['id'])) {//->allowField(true)->validate(true)
            if ($param['type'] == 1 ) {

                $paths = $this->uploads();
                if (!$paths) return false;
                $param['path'] = implode(',', $paths);
            }
            if ($param['type'] == 2 ) {
                if ( !in_array($param['source'], ['GIT','TFS','SVN','Perforce'])) {
                    $this->error('远程抓取请选择类型');
                    return false;
                }
                if (empty($param['route'])) {
                    $this->error('远程抓取请选择路径');
                    return false;
                }
                switch ($param['source']) {
                    case 'GIT':

                        /*exec("cd git && git clone https://".$param['proof_user'].":".$param['proof_pwd']."@".$param['route']);
                        $zipname = 'path/test.zip';  */
                       /* $files = 'git/';
                        creatZip($zipname, $files);*/
                        break;
                    
                }
            }
            if (!$this->save($param)) {
                empty($this->error) and $this->error = '添加失败';
                return false;
            }
        } else {
            if (!$this->save($param, ['id'=>$param['id']])) {
                empty($this->error) and $this->error = '保存失败';
                return false;
            }
        }
        return $this->id;
    }

    /**
     * 上传附件
     */
    public function uploads($file='file', $size=2097152, $type='zip')
   {
       
        $data = [];
        $files = input('file.');
        // dump($files);die;
        if (empty($files)) {
            $this->error = '请选择上传文件';
            return false;
        }
        if (is_array($files)) {
            $path = 'uploads/temp';
            
            foreach ($files as $file) {
                // 移动到框架应用根目录/uploads/ 目录下
                $info = $file->validate(['size'=>$size,'ext'=>$type])->move($path,true);
                if($info){
                    $path_name = str_replace('\\','/',$info->getpathName());
                    // $fileName = str_replace('\\','/',$info->getfileName());
                    
                    $data[] =  $path_name;
                        
                       /* 'name' => $info->getInfo()['name'],
                        'size' => $info->getInfo()['size'],
                        'path' => $path*/
                    
                }else{
                    $this->error = $file->getError();
                    return false;
                }
            }
        }else{
            $path = 'uploads/temp';
            
            $info = $files->validate(['size'=>$size,'ext'=>$type])->move($path, true);
            if ($info) {
                
                $path_name = str_replace('\\','/',$info->getpathName());
                // $fileName = str_replace('\\','/',$info->getfileName());
                
                $data[] =  $path_name;
                    // 'name' => $info->getInfo()['name'],
                    // 'size' => $info->getInfo()['size'],
                    // 'path' => $path,
                
            } else {
                $this->error = $files->getError();
                return false;
            }
        }
        return $data;
    }
}